/*
 * Main Custodian menu
 * Vic.Sorbi
 */
package ganga.custodian;

import ganga.custodian.ui.Builder;
import ganga.custodian.ui.ListViewAdapter;
import ganga.custodian.ui.ListViewItem;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

/**
 * This activity aimed to be a main user menu screen
 * 
 * @author Apocrypha
 * @created at 15.05.2011
 */
public class MainActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainactivity);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		buildListView(R.id.listView1);

		Button alarmButton = (Button) findViewById(R.id.alarmButton);
		alarmButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				doAlarm();
			}
		});
	}
	@Override
	protected void onResume() {
		super.onResume();
		setCurrentTheme();
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {

		case BluetoothManager.ENABLE_BT_REQUEST:
			//TODO Proceed result
			String str = data.getDataString();
			setTitle(str);
			Log.i("BT", Integer.toString(resultCode));
			break;
		default:
			super.onActivityResult(requestCode, resultCode, data);
		}
	}

	private void setCurrentTheme() {
		ListView Mainlist = (ListView) findViewById(R.id.listView1);
		LinearLayout MainLayout =(LinearLayout) findViewById(R.id.mainMenuLayout);
		Mainlist.setCacheColorHint(CustodianApplication.getInstance().selectTheme.backGround);
		Mainlist.setBackgroundColor(CustodianApplication.getInstance().selectTheme.backGround);
		MainLayout.setBackgroundColor(CustodianApplication.getInstance().selectTheme.backGround);
	}

	protected void doAlarm() {
		application.notificationExecutor.sendSms();
		// CustodianApplication.getInstance().notificationExecutor.play();
		application.notificationExecutor.doPlayTaskManFillBad();
		application.notificationExecutor.call();
	}

	//private section

	//builder
	private void buildListView(int listVewId) {
		ListView listView = (ListView) findViewById(listVewId);

		//Создать список из элементов 
		final List<ListViewItem> itemsList = new ArrayList<ListViewItem>();
		itemsList.add(new ListViewItem("Графики", "Показания ЧСС и КИГ", R.drawable.chart2));
		itemsList.add(new ListViewItem("Настройка", "Настройка датчиков и персонализация", R.drawable.elecrto));
		itemsList.add(new ListViewItem("Монитор реального времени", "Текущие показания ЧСС и КИГ", R.drawable.pulse));
		
		//Проинициализировать отображаемый список
		ListViewAdapter adapter = new ListViewAdapter(this, itemsList);
		listView.setAdapter(adapter);

		listView.setClickable(true);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position, long index) {
				startActivityByPosition(position);
			}
		});
	}

	private void startActivityByPosition(int position) {
		Intent intent;
		switch (position) {
		case (0): {
			intent = new Intent(this, ganga.custodian.plot.PlotActivity.class);
			break;
		}
		case (1): {
			intent = new Intent(this, ganga.custodian.settings.MainSettingsActivity.class);
			break;
		}
		case (2): {
			intent = new Intent(this, ganga.custodian.dashboard.MonitorScreenActivity.class);
			break;
		}
		default: {
			return;
		}
		}
		startActivity(intent);
	}

	// The application context is the central location for all top-level
	// application functionality.
	private CustodianApplication application = CustodianApplication.getInstance();
	private Builder builder = new Builder(this);
}